package com.xin.data.simbapesa.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;

import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;

import java.lang.reflect.Field;


public class BarUtils {

    @SuppressLint({"InternalInsetResource", "DiscouragedApi"})
    public static int getStatusBarHeight(Context context) {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return context.getResources().getDimensionPixelSize(resourceId);
        }
        return (int) (24 * context.getResources().getDisplayMetrics().density);
    }

    @SuppressLint("PrivateApi")
    public static int getStatusBarHeightByReflection(Context context) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object obj = clazz.newInstance();
            Field field = clazz.getField("status_bar_height");
            Object obj1 = field.get(obj);
            if (obj1 != null) {
                int resourceId = Integer.parseInt(obj1.toString());
                return context.getResources().getDimensionPixelSize(resourceId);
            }
            return 0;
        } catch (Exception e) {
            return 0;
        }
    }

    public static int getStatusBarHeightViaInsets(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        WindowInsets insets = decorView.getRootWindowInsets();
        if (insets != null) {
            return insets.getSystemWindowInsetTop();
        }
        return 0;
    }


    public static void showLightStatusBars(Activity activity, boolean isLieght) {
        WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView()).setAppearanceLightStatusBars(isLieght);
    }

    public static void setNavigationBar(Activity activity, boolean isHide) {
//        if (isHide) {
//            Window window = activity.getWindow();
//            window.getDecorView().setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//            WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView()).hide(WindowInsetsCompat.Type.navigationBars());
//        } else {
//            WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView()).show(WindowInsetsCompat.Type.navigationBars());
//        }
    }

    public static void setNavigationBarColor(Activity activity, int colorId) {
        activity.getWindow().setNavigationBarColor(ContextCompat.getColor(activity, colorId));
    }

    public static void setStatusBarColor(Activity activity, int colorId) {

        LogUtil.e("me", "--setStatusBarColor--");

        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(colorId);

    }

}
